const { series, parallel, src, dest } = require('gulp')
// https://www.gulpjs.com.cn/docs/getting-started/quick-start/
// series顺序串联
// parallel并发串联
const zip = require('gulp-zip')
const Client = require('ftp')
const ChatBot = require('dingtalk-robot-sender')
// todo: husky  git hook https://www.npmjs.com/package/husky

// 直接使用 webhook
const robot = new ChatBot({
  webhook: 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx',
})

// 上传ftp
function ftpUpload() {
  const config = {
    host: 'coding.ftp-gz01.bcehost.com',
    port: 8010,
    user: 'coding',
    password: 'uV6G1zmI8J%mZUhS',
  }
  const client = new Client()
  client.on('ready', function () {
    client.put('./build.zip', './temp/build-remote.zip', function (err) {
      if (err) throw err
      client.end()
    })
  })
  client.connect(config)
}

// 钉钉机器人消息
function dingMsg() {
  // https://ding-doc.dingtalk.com/doc#/serverapi2/gh60vz
  let textContent = {
    msgtype: 'link',
    link: {
      title: '123',
      text: '',
      picUrl: '',
      messageUrl: '',
    },
  }
  robot.send(textContent).then((res) => {
    console.log(res)
  })
}

// 打包
function build() {
  return src('src/**/*')
    .pipe(dest('dist'))
    .pipe(zip('build.zip'))
    .pipe(dest('.'))
}

function defaultTask(cb) {
  cb()
  console.log('默认任务')
}

// 公开任务
exports.build = series(build, ftpUpload)
exports.upload = ftpUpload
// exports.default = parallel(build, defaultTask)

/*
const { series, parallel } = require('gulp');
function clean(cb) {
  // body omitted
  cb();
}
function css(cb) {
  // body omitted
  cb();
}
function javascript(cb) {
  // body omitted
  cb();
}
exports.build = series(clean, parallel(css, javascript));
*/
